﻿<script>
    var _sorts = @this.RawSerialize(this.ViewBag.ListSort);
    var _parts=@this.RawSerialize(this.ViewBag.PartList);
    var _file_domain = "@ViewBag.FileDomain";
     

     



    var _vm;
    $(function () {
        var vm = new PageViewModel();
        _vm = vm;
        ko.applyBindings(vm);
        vm.init();

    });
 

    function PageViewModel()
    {
        var me = this;
        ViewModel.call(me);
        vmExtend.call(me);

        me.url = "/Wiki/Shop/Models";
        me.deleteUrl = "/Wiki/Shop/Delete";

        
        me.dialog = new FormDialog(me);

        me.revisePasswordDialog = new RevisePasswordDialog();
        me.openRevisePasswordDialog = function () {
            me.revisePasswordDialog.open(me.dataTable.selectedModel());
        }
         
    }
    function vmExtend() {
        var me = this;
    }


    function FormDialog(vm) {
        var me = this;
        Dialog.call(me);

        me.onOpen = function () {

             

            var model = me.editModel();
            if (model)
            {
                var bindModel = $ko.toJS(model);
                me.model(bindModel);
                  
                $("#thelist").empty();
                if (bindModel.ShopUrl != "")
                { 
                    $("#thelist").append("<img class='img_thum' src='" + bindModel.ShopUrl + "' />");
                }
            }
            else
            {
                $("#ShopUrl").val("");
                $("#thelist").empty();  
                me.model({ IsEnabled: true });
            }
        }

        me.onSave = function () {
            if (!$('#form1').formValid()) {
                return false;
            }

            var model = me.model();
            model.GPS_X = 0;
            model.GPS_Y = 0;

            if (me.editModel()) {
                model.ShopUrl = $("#ShopUrl").val(); 
                $ace.post("/Wiki/Shop/Edit", model, function (result) {
                    $ace.msg(result.Msg);
                    me.close();
                    vm.dataTable.reload();
                    //setTimeout(function () { window.location.reload(); }, 1000);
                }
                );
            }
            else {
                model.ShopUrl = $("#ShopUrl").val(); 
                $ace.post("/Wiki/Shop/Add", model, function (result) {
                    $ace.msg(result.Msg);
                    me.close();
                    vm.loadData();
                    //setTimeout(function () { window.location.reload(); }, 1000);
                }
                );
            }
        }
    }


    function RevisePasswordDialog()
    {
        var me = this;
        Dialog.call(me);
        me.title("商家程序码");

        me.onOpen = function ()
        {
            var model = me.editModel();
            if (!model)
            {
                throw new Error();
            }

            var bindModel = $ko.toJS(model);
            console.log(bindModel);
            var shopid = bindModel.ID;
            $ace.get("/API/QRCode", { ShopID: shopid}, function (result)
            {
                console.log(result);
                $("#shopcode").attr("src", result.Data);
            });

            //var m = {};
            //m.Id = bindModel.Id;
            //m.Mobile = bindModel.Mobile;
            //m.UserName = bindModel.UserName;

            //me.model(m);
        }
        me.onSave = function ()
        {
            me.close();
        }
    }
</script>
